package com.example.nisseapp;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Button;

import com.google.android.gcm.GCMBaseIntentService;


/**	 Class Overview (GCMBaseIntentService)
 * 	 Skeleton for application-specific IntentServices responsible for handling 
 * 	 communication from Google Cloud Messaging service. 

	 The abstract methods in this class are called from its worker thread, 
	 and hence should run in a limited amount of time. If they execute long operations, 
	 they should spawn new threads, otherwise the worker thread will be blocked. 

	 Subclasses must provide a public no-arg constructor. 
 * 
 * */	

public class GCMIntentService extends GCMBaseIntentService 
{
	public GCMIntentService() 
	{
		// TODO Auto-generated constructor stub
		//super("403299226305");
	}
	
	/**
	 * Called after a registration intent is received, passes the registration ID 
	 * assigned by GCM to that device/application pair as parameter. 
	 * Typically, you should send the regid to your server so it can use it to send messages 
	 * to this device.
	 */
	@Override
	public void onRegistered(Context context, String regId)
	{
		Log.d("onRegistered", regId);
		System.out.println("RegID from class GCMIntent: " + regId);
		//wcfConn.SaveRegId(regId);
	}
	
	/**
	 * Called when the device tries to register or unregister, but GCM returned an error. 
	 * Typically, there is nothing to be done other than evaluating the error 
	 * (returned by errorId) and trying to fix the problem.
	 * 
	 * Called on registration or unregistration error.
	 * 
	   Parameters 
	   context: application's context.
	   errorId: error id returned by the GCM service. 
	 * */	
	@Override	
	protected void onError(Context arg0, String error) 
	{	       
		Log.d("Error returned by GCM Service", "onError: " + error);	
	}
	
	/**
	 * Called when the device tries to register or unregister, but the GCM servers 
	 * are unavailable. The GCM library will retry the operation using exponential 
	 * backup, unless this method is overridden and returns false. This method is 
	 * optional and should be overridden only if you want to display the message 
	 * to the user or cancel the retry attempts. 
	 */
	@Override
    protected boolean onRecoverableError(Context context, String errorId){
        Log.d("onRecoverableError", errorId);
        return false;
    }

	/**
	 * Called when the server sends a message to GCM, and GCM delivers it 
	 * to the device. If the message has a payload, its contents are available 
	 * as extras in the intent.
	 */
    @Override
    protected void onMessage(Context context, Intent intent) 
    {
        Log.d("onMessage", String.valueOf(intent));
        
        String message = intent.getStringExtra("message");		
        long timestamp = intent.getLongExtra("timestamp", -1);	
        System.out.println("Push-besked: " + message);
        
        sendGCMIntent(context, message);  
    }

    /**
     * Called after the device has been unregistered from GCM. 
     * Typically, you should send the regid to the server so it unregisters the device.
     */
    @Override
    protected void onUnregistered(Context context, String regId) 
    {
        Log.d("onUnregistered", regId);
    }
    
    private void sendGCMIntent(Context context, String message) 
    {
		
		Intent broadcastIntent = new Intent();
		broadcastIntent.setAction("GCM_RECEIVED_ACTION");
		
		broadcastIntent.putExtra("gcm", message);
		
		context.sendBroadcast(broadcastIntent);		
	}

}

